﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Xml;

namespace fbasync
{
    [DataContract, Serializable]
    public class StoryTemplateBundle
        : FacebookEntity
    {
        [DataMember]
        public long? ID { get; set; }

        [DataMember]
        public DateTime? Created { get; set; }

        [DataMember]
        public IList<StoryTemplate> OneLineStories { get; set; }

        [DataMember]
        public IList<StoryTemplate> ShortStories { get; set; }

        [DataMember]
        public StoryTemplate FullStory { get; set; }

        [DataMember]
        public IList<ActionLink> ActionLinks { get; set; }

        public static IList<StoryTemplateBundle> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            List<StoryTemplateBundle> results = fqlResult.GetFqlResults(keepXml, e =>
                new StoryTemplateBundle
                {
                    OneLineStories = new List<StoryTemplate>(StoryTemplate.FromContainerElement(e["one_line_story_templates", FacebookConnection.FacebookNamespace])),
                    ShortStories = new List<StoryTemplate>(StoryTemplate.FromContainerElement(e["short_story_templates", FacebookConnection.FacebookNamespace])),
                    FullStory = StoryTemplate.FromElement(e["full_story_template", FacebookConnection.FacebookNamespace]),
                    ID = e.GetChildLong("template_bundle_id"),
                    Created = e.GetChildUnixTime("time_created"),
                    ActionLinks = new List<ActionLink>(ActionLink.FromContainerElement(e["action_links", FacebookConnection.FacebookNamespace])),
                });
            return results;
        }

        public static StoryTemplateBundle FromSingleResult(XmlDocument singleResult, bool keepXml)
        {
            StoryTemplateBundle result = singleResult.GetSingleResult(keepXml, e =>
                new StoryTemplateBundle
                {
                    OneLineStories = new List<StoryTemplate>(StoryTemplate.FromContainerElement(e["one_line_story_templates", FacebookConnection.FacebookNamespace])),
                    ShortStories = new List<StoryTemplate>(StoryTemplate.FromContainerElement(e["short_story_templates", FacebookConnection.FacebookNamespace])),
                    FullStory = StoryTemplate.FromElement(e["full_story_template", FacebookConnection.FacebookNamespace]),
                    ID = e.GetChildLong("template_bundle_id"),
                    Created = e.GetChildUnixTime("time_created"),
                    ActionLinks = new List<ActionLink>(ActionLink.FromContainerElement(e["action_links", FacebookConnection.FacebookNamespace])),
                });
            return result;
        }
    }
}
